package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "用户购物车相关接口")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation(value = "添加购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车，购物车信息：{}",shoppingCartDTO);

        //调用service层，添加购物车
        shoppingCartService.addShoppingCart(shoppingCartDTO);

        //返回结果
        return Result.success();
    }

    /**
     * 根据用户id查询购物车
     * @return
     */
    @ApiOperation(value = "根据用户id查询购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        log.info("根据用户id查询购物车");

        //调用service层，根据用户id查询购物车
        List<ShoppingCart> list = shoppingCartService.list();

        //返回结果
        return Result.success(list);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation(value = "删除购物车中的一个商品")
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中的一个商品，购物车信息：{}",shoppingCartDTO);

        //调用service层删除
        shoppingCartService.sub(shoppingCartDTO);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 清空用户购物车
     * @return
     */
    @ApiOperation(value = "清空购物车")
    @DeleteMapping("/clean")
    public Result clean(){
        log.info("清空用户购物车");

        //调用service层，清空购物车
        shoppingCartService.clean();

        //返回结果给前端
        return Result.success();
    }
}
